import { getAllComponyApi, getFieldTypesApi } from '@api/common'

const optionsStore = {
  state: {
    companyOptions: undefined,
    fieldModelTypes: undefined,
    // 角色类型
    // 案源机构
    caseCompanyOptions: undefined,
    // 委外机构
    outsideCompanyOptions: undefined,
    // 司法机构
    justiceCompanyOptions: undefined,
    // 通用模板
    caseTemplateOptions: undefined,
    // 可用策略
    strategyOptions: undefined,
    // 标签
    collectTagOptions: undefined
  },
  mutations: {
    SET_COMPANY_OPTIONS(state: any, value: any) {
      state.companyOptions = value
    },
    SET_FIELD_TYPES(state: any, value: any) {
      state.fieldModelTypes = value
    },
    SET_CASE_COMPANY_OPTIONS(state: any, value: any) {
      state.caseCompanyOptions = value
    },
    SET_OUTSIDE_COMPANY_OPTIONS(state: any, value: any) {
      state.outsideCompanyOptions = value
    },
    SET_JUSTICE_COMPANY_OPTIONS(state: any, value: any) {
      state.justiceCompanyOptions = value
    },
    SET_CASE_TEMPLATE_OPTIONS(state: any, value: any) {
      state.caseTemplateOptions = value
    },
    SET_STRATEGY_OPTIONS(state: any, value: any) {
      state.strategyOptions = value
    },
    SET_COLLECT_TAG_OPTIONS(state: any, value: any) {
      state.collectTagOptions = value
    }
  },
  actions: {
    getCompanyOptions({ commit, state }: any, refresh = false) {
      return new Promise((resolve, reject) => {
        if (state.companyOptions !== undefined && !refresh) {
          resolve(state.companyOptions)
        } else {
          (getAllComponyApi() as any)
            .then((res: [{ apiCode: string, companyName: string}]) => {
              const options = res.map(c => ({ ...c, value: c.apiCode, label: `${c.companyName}-${c.apiCode}` }))
              commit('SET_COMPANY_OPTIONS', options)
              resolve(options)
            })
            .catch((err: Error) => {
              reject(err)
            })
        }
      })
    },
    getFieldModelTypes({ commit, state }: any) {
      return new Promise((resolve, reject) => {
        if (state.fieldModelTypes !== undefined) {
          resolve(state.fieldModelTypes)
        } else {
          (getFieldTypesApi() as any)
            .then((res: { variableType: [{ value: string, name: string }] }) => {
              const options = res.variableType.map(c => ({ value: c.value, label: c.name }))
              commit('SET_FIELD_TYPES', options)
              resolve(options)
            })
            .catch((err: Error) => {
              reject(err)
            })
        }
      })
    },
    // 清除指定机构缓存
    clearCompanyStore({ commit }: any, type: number) {
      switch (type) {
        case 1:
          commit('SET_CASE_COMPANY_OPTIONS', undefined)
          break
        case 2:
          commit('SET_OUTSIDE_COMPANY_OPTIONS', undefined)
          break
        case 3:
          commit('SET_JUSTICE_COMPANY_OPTIONS', undefined)
          break
        default:
          break
      }
    },
    // 清除案件模板缓存
    clearCaseTemplateStore({ commit }: any) {
      commit('SET_CASE_TEMPLATE_OPTIONS', undefined)
    },
    // 清除可用策略
    clearStrategyStore({ commit }: any) {
      commit('SET_STRATEGY_OPTIONS', undefined)
    },
    // 清除标签缓存
    clearCollectTagsStore({ commit }: any) {
      commit('SET_COLLECT_TAG_OPTIONS', undefined)
    }
  },
  getters: {
    companyOptions: (state: any) => state.companyOptions,
    caseTemplateOptions: (state: any) => state.caseCompanyOptions,
    strategyOptions: (state: any) => state.strategyOptions
  }
}

export default optionsStore
